package com.cupdata.zicon.jdk7concurrent.chapter1;

import java.lang.Thread.UncaughtExceptionHandler;

/**
When an uncaught exception is thrown in Thread, the JVM looks for 
three possible handlers for this exception.

   First, it looks for the uncaught exception handler of the Thread objects as we learned in this
recipe. If this handler doesn't exist, 
   then the JVM looks for the uncaught exception handler
for ThreadGroup of the Thread objects as was explained in the Processing uncontrolled
exceptions in a group of threads recipe.
   If this method doesn't exist, the JVM looks for the
default uncaught exception handler as we learned in this recipe.

If none of the handlers exits, the JVM writes the stack trace of the exception in the console
and exits the program.

 * 
 * @author SunYabing
 *
 */
public class UncaughtExceptionHandlerTest {

	//@Test
	public static void  main(String[] args) {
		Task task = new Task();
		Thread thread = new Thread(task);
		
		thread.setUncaughtExceptionHandler(new ExceptionHandler());
		thread.start();
	}

}

class ExceptionHandler implements UncaughtExceptionHandler {

	@Override
	public void uncaughtException(Thread t, Throwable e) {
		System.out.printf("An exception has been captured\n");
		System.out.printf("Thread: %s\n", t.getId());
		System.out.printf("Exception: %s: %s\n", e.getClass().getName(),
				e.getMessage());
		System.out.printf("Stack Trace: \n");
		e.printStackTrace(System.out);
		System.out.printf("Thread status: %s\n", t.getState());
	}

}

class Task implements Runnable {
	@Override
	public void run() {
		int numero = Integer.parseInt("TTT");
	}
}
